﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace HR60_PanelSoft.View.Utils
{
    //GC: [g0]设计通用进度条[ComProgressBar]@s1
    /* 
     * 设计通用进度条，使之成为通用的、各窗体可控的 
     */
    //GC: [g1]增加对通讯接口的调用
    //GC: [g2]联调[ComProgressBar]
    //GC: [g3]联调7-19-1,修正显示bug
    //GC: [g4]重构7-19-2,重构进度条bug
    //GC: [g5]bug2620[ComProgressBar]

    /* [g0.s1] */
    public partial class ComProgressBar : Form,HR60_PanelSoft.NetWork.IProcessHandle
    {
        /// <summary>
        /// 
        /// </summary>
        //用来传递调用者的位置；
        private Form _parent = null;

        public bool IsSucessful = true;
        
        /* [g4] */
        private static bool IsRunning = false;
        /* end of [g4] */

        /* [g1.s1]增加最大值常量 */
        /* [g2.s1] */
        //public const int MaxValue = 10;
        public const int MaxValue = 99;
        /* end of [g2.s1] */
        /*  */
        

        /* [g1.s3]根据通讯接口优化 */
        public int Value
        {
            get
            {
                return this.PRGB_ONMAIN_PROGRESS.Value;
            }
            set
            {
                if (value < 0)
                {
                    this.PRGB_ONMAIN_PROGRESS.Value = 0;
                    this.LBL_ONMAIN_PERCENT.Text = "0%";
                }

                //else if (value > 9)
                else if(value > MaxValue - 1)
                {
                    //this.PRGB_ONMAIN_PROGRESS.Value = 10;
                    this.PRGB_ONMAIN_PROGRESS.Value = MaxValue;
                    this.LBL_ONMAIN_PERCENT.Text = "100%";
                    this.Compeleted();                    
                }
                else
                {
                    this.PRGB_ONMAIN_PROGRESS.Value = value;
                    this.LBL_ONMAIN_PERCENT.Text = (value * 10).ToString() + "%";
                }
            }
        }

        /* end of [g1.s3] */

        public ComProgressBar(Form parent,string title)
        {
            if (ComProgressBar.IsRunning)
                return;

            InitializeComponent();
            this.Text = title;
            this._parent = parent;
            this.StartPosition = FormStartPosition.Manual;

            this.LBL_ONMAIN_PERCENT.Parent = this.PRGB_ONMAIN_PROGRESS;
            this.LBL_ONMAIN_PERCENT.Location = new Point((this.Size.Width - LBL_ONMAIN_PERCENT.Size.Width) / 2,
                (this.Size.Height - LBL_ONMAIN_PERCENT.Size.Height) / 2);

            CheckForIllegalCrossThreadCalls = false;
            /* [g1.s2] 设置最大值 */
            //this.PRGB_ONMAIN_PROGRESS.Maximum = 10;
            this.PRGB_ONMAIN_PROGRESS.Maximum = MaxValue;
            /* end of [g1.s2] */

        }

        private void Compeleted()
        {
            System.Threading.Thread.Sleep(500);
            ComProgressBar.IsRunning = false;
            this.Close();
            this.Dispose();
        }

        private Point getLocation(Point location, Size size)
        {
            Point rst = new Point();
            rst.X = location.X + ( size.Width - this.Size.Width )/2;
            rst.Y = location.Y + (size.Height - this.Size.Height) / 2;
            return rst;
        }

        private void LocatCenter()
        {
            this.Location = getLocation(this._parent.Location, this._parent.Size);

        }

        public void Start()
        {
            ComProgressBar.IsRunning = true;

            Thread tempT = new Thread(new ThreadStart(() =>
                {
                    this.Value = 0;
                    this.LocatCenter();
                    this.ShowDialog();
                }));
            tempT.Start();

        }

        public void Stop()
        {
            ComProgressBar.IsRunning = false;
            this.Close();
            this.Dispose();
            /* [g5.s1] */
            this._parent.Focus();
            /* end of [g5.s1] */
        }

        

        public void onError(string errStr)
        {
            this.IsSucessful = false;
            this.Value = ComProgressBar.MaxValue;
            View.ViewUtils.showAlert(errStr);
        }

        public void onProcess(int percent)
        {
            this.Value = percent;
        }
       
    }

    /* end of [g0.s1] */
}
